package com.codinghero.zqlb.spider.handler;

import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import com.codinghero.zqlb.match.MatchUtils;
import com.codinghero.zqlb.match.SoccerMatch;

/**
 * 过滤球队名称、转播频道中的错误信息（包括错误信息和垃圾信息）
 * 
 * @author liutong01
 * 
 */
public class JunkFilterHandler implements MatchHandler {

	public <T extends SoccerMatch> void handle(List<T> list) {

		Iterator<T> matchIter = list.iterator();
		while(matchIter.hasNext()) {
			SoccerMatch match = matchIter.next();

			// 如果球队信息中出现了转播信息，则将该信息移至转播信息字段
			// 如果包含了垃圾信息，则置为空值
			Entry<String, Integer> e1 = MatchUtils.contains(
					SoccerMatch.CHANNEL_MAP, match.getTeam1Name());
			if (e1 != null) {
				match.getChannelNameList().add(match.getTeam1Name());
				match.setTeam1Name("");
			} else {
				Entry<String, Integer> e = MatchUtils.contains(
						SoccerMatch.JUNK_MAP, match.getTeam1Name());
				if (e != null)
					match.setTeam1Name("");
			}

			Entry<String, Integer> e2 = MatchUtils.contains(
					SoccerMatch.CHANNEL_MAP, match.getTeam2Name());
			if (e2 != null) {
				match.getChannelNameList().add(match.getTeam2Name());
				match.setTeam2Name("");
			} else {
				Entry<String, Integer> e = MatchUtils.contains(
						SoccerMatch.JUNK_MAP, match.getTeam2Name());
				if (e != null)
					match.setTeam2Name("");
			}
			
			// 过滤转播信息
			Iterator<String> channelIter = match.getChannelNameList().iterator();
			while (channelIter.hasNext()) {
				String channel = channelIter.next();
				Entry<String, Integer> e = MatchUtils.contains(
						SoccerMatch.JUNK_MAP, channel);
				if (e != null)
					channelIter.remove();
			}
			match.setChannelNames(match.getChannelNameList());
		}
	}

}
